/**
 * 
 */
package com.et114.modules.products.securitydoor.action;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.et114.components.simpletable.DataList;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.AppContext;
import com.et114.core.common.CONST;
import com.et114.core.utility.FileUploadUtil;
import com.et114.core.utility.NumberUtils;
import com.et114.core.utility.StringPool;
import com.et114.core.utility.ThreadSynchronizer;
import com.et114.modules.products.commons.Const;
//import com.et114.modules.products.commons.Context;
import com.et114.modules.products.commons.PorJsonObj;
import com.et114.modules.products.procontent.services.ProContentServiceImpl;
import com.et114.modules.products.procontent.vo.ProContent;
import com.et114.modules.products.securitydoor.services.SecuritySystemServiceImpl;
import com.et114.modules.products.securitydoor.vo.SecurityDoor;
import com.et114.modules.products.securitydoor.vo.SecuritySystemType;
import com.et114.modules.products.supplier.services.SupplierServiceImpl;
import com.et114.modules.products.supplier.vo.ProSuppliers;
import com.et114.modules.system.dictionary.DictionaryCache;
import com.et114.modules.system.dictionary.vo.BizOption;

/*
 * @author guanhw
 */
public class SecuritySystemAction extends BaseDispatchAction {


	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getSecuritySystem( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_SS_Type_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;
		
		
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		ProSuppliers psvo = new ProSuppliers( ) ; 
		psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_SS + StringPool.COMMA ) ; 
		psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) ) ;
		List< ProSuppliers > proTypes = ssbean.querySuppliersList ( psvo ) ; 
		request.setAttribute ( "proTypes" , proTypes ) ;
		//BIS
		SecuritySystemServiceImpl sss = ( SecuritySystemServiceImpl )getBean ( "securitySystemServiceImpl" ) ;
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl ) getBean ( "proContentServiceImpl" );		
		
		SecuritySystemForm securitySystemForm = ( SecuritySystemForm ) form ; 
		SecurityDoor secrityDoor = new SecurityDoor( ) ; 
		PropertyUtils.copyProperties ( secrityDoor , securitySystemForm ) ; 
		
		request.setAttribute ( "searchVo" , secrityDoor ) ;
		
		
		Long sdid = securitySystemForm.getProId ( ) ; 
		if ( sdid != null && sdid.longValue ( ) != 0  ) { // load one
			secrityDoor = sss.getSecuritySystem (  sdid ) ; 
			request.setAttribute ( "secrityDoor" , secrityDoor ) ;
			
			//get content
			ProContent pcontent = proContentServiceImpl
					.getProContent ( new ProContent ( Byte
							.valueOf ( Const.PRO_TYPE_SS ) , sdid ) );
			if ( pcontent != null )
				request.setAttribute ( "content" , pcontent );
			
			return mapping.findForward ( "sDoor" ) ; 
		}
		else { //loads
			DataList dl = sss.getSecuritySystems ( secrityDoor ) ; 
			List< SecurityDoor  > sds = dl.getNowlist ( ) ; 
			SecuritySystemType ssType = null ; 
			for ( SecurityDoor sd : sds ) {
				sd.setSsType ( null  ) ; 
				for ( BizOption bioCt : bssOps ) {
					if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
							sd.getSdtypeId ( ) ) ) {
						ssType = new SecuritySystemType( ) ; 
						ssType.setSdTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
						ssType.setSdTypeName ( bioCt.getOptionName ( ) ) ;
						sd.setSsType ( ssType ) ;
						break ; 
					}
				}				
			}
			
			request.setAttribute ( "dl" , dl ) ;  
			return mapping.findForward ( "sDoors" ) ; 
		}
		
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward toEditSecuritySystem( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_SS_Type_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , CONST.COUNTRY_ZH  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;
		
		SupplierServiceImpl ssbean = ( SupplierServiceImpl )getBean ( "supplierServiceImpl" ) ;
		ProSuppliers psvo = new ProSuppliers( ) ; 
		psvo.setWorkFlg ( String.valueOf ( Const.COMMON_STATUS_ENA ) ) ;
		psvo.setSupplierPros ( StringPool.COMMA + Const.PRO_TYPE_SS + StringPool.COMMA ) ; 
		List< ProSuppliers > proTypes = ssbean.querySuppliersList ( psvo ) ; 
		request.setAttribute ( "proTypes" , proTypes ) ;
		
		return mapping.findForward ( "sDoor" ) ; 
	}
	
	
	
	public  ActionForward editSecuritySystem( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		SecuritySystemForm securitySystemForm = ( SecuritySystemForm ) form ; 
		//DIC
		
		//PARAMS 
		
		//BIS
		SecuritySystemServiceImpl sss = ( SecuritySystemServiceImpl )getBean ( "securitySystemServiceImpl" ) ; 
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl ) getBean ( "proContentServiceImpl" );

		SecurityDoor secrityDoor = new SecurityDoor( ) ; 
		PropertyUtils.copyProperties ( secrityDoor , securitySystemForm ) ; 
		
		secrityDoor.setUpdtUser ( getCurrentUser ( request ).getUserId ( ) ) ;
		Long id = securitySystemForm.getProId ( ) ; 
		
		Byte actStatus = securitySystemForm.getProActStatus ( ) ; 
		if (actStatus != null ) {
			if ( Const.PRO_ACTION_SUB == actStatus ) {
				if ( NumberUtils.isEmptyOrZero ( id ) )
					secrityDoor.setCrtUser ( getCurrentUser ( request )
							.getUserId ( ) );
				id = saveAndUpdateSecurity ( secrityDoor , securitySystemForm
						.getSsImageff ( ) , sss , request );
			}
			else if ( Const.PRO_ACTION_PUB == actStatus ) {
				secrityDoor = sss.getSecuritySystem ( id ) ;
				secrityDoor.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED ) ; 
				id = saveAndUpdateSecurity( secrityDoor , securitySystemForm.getSsImageff ( ) , sss , request ) ; 
			}
			
			else if ( Const.PRO_ACTION_SUB_CHECK == actStatus ) {
				secrityDoor = sss.getSecuritySystem ( id ) ; 
				Date offTime = securitySystemForm.getOffTime ( ) ; 
				if (  ( offTime != null && ( new Date ( )
								.getTime ( )- offTime.getTime ( ) >= 0 ) ) ) 			
					return PorJsonObj.response ( response , PorJsonObj.error ( "offTime" ) );				

				return PorJsonObj.response ( response , PorJsonObj.success (  "!" ) );	
			}
			
			else if ( Const.PRO_ACTION_SUB_PUB == actStatus ) {
				secrityDoor.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED );
				if ( id != null && id.longValue ( ) != 0 ) // update
					id = saveAndUpdateSecurity( secrityDoor , securitySystemForm.getSsImageff ( ) , sss , request ) ; 
			}
			 
			else if ( Const.PRO_ACTION_YULAN == actStatus ) {
				
			}
			else if ( Const.PRO_ACTION_OUT == actStatus ) {
				//只更新 下架状态
				SecurityDoor osd = sss.getSecuritySystem ( id ) ; 
				Byte bos = osd.getProOnOffStatus ( ) ; 
				if ( !NumberUtils.isEmptyOrZero ( bos ) &&  Const.PRO_PUB_STATUS_PUBED == bos.byteValue ( )  ) {
					osd.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ; 
					sss.updateSecuritySystems ( osd ) ; 									
				}
			}			
			
			//save content
			if ( Const.PRO_ACTION_SUB == actStatus || Const.PRO_ACTION_SUB_PUB == actStatus ) { 
				ProContent content = new ProContent( Byte.valueOf ( Const.PRO_TYPE_SS ) , id )  ; 
				content.setContent ( securitySystemForm.getContent ( ) ) ; 
				proContentServiceImpl.saveAndUpdateProContent ( content ) ;		
			}
		}
		//Context.indexReload ( ) ; 
		return new ActionForward( "securitySystem.go?method=getSecuritySystem&proId=" + id , true ); 
	}	
	
	
	
	private Long saveAndUpdateSecurity( SecurityDoor dtVo , FormFile formFile  , SecuritySystemServiceImpl sss , HttpServletRequest request ) throws Exception {
		Long proId = dtVo.getProId ( ) ; 
		String uploadFn = formFile.getFileName ( ); 
		if ( !StringUtils.isEmpty ( uploadFn ) && !StringUtils.isBlank ( uploadFn ) )   {
			FileUploadUtil upload = new FileUploadUtil( ) ; 
			ThreadSynchronizer.threadEnter ( );
			try {
				upload.setRestrictfs ( 102400 * 5 ) ;
				String fn = upload.uploadv2 ( formFile , request , Const.PRO_SECURITY_IMG_UPLOAD_PATH , UUID.randomUUID ( ).toString ( ) );
				fn = Const.PRO_SECURITY_IMG_UPLOAD_PATH + fn;
				dtVo.setSsImage ( fn ) ; 	
			} finally {
				ThreadSynchronizer.threadExit ( );
			}						
		}
		
		if ( !NumberUtils.isEmptyOrZero ( proId )  ) { // update
			SecurityDoor dt = sss.getSecuritySystem ( proId ) ; 
			String dtImg =  dt.getSsImage ( ) ; 
			if ( StringUtils.isEmpty ( uploadFn ) || StringUtils.isBlank ( uploadFn ) ) 
				dtVo.setSsImage ( dtImg  ) ; 
			else {
				FileUploadUtil.removeRealPathFile ( dtImg , request ) ;
			}
			sss.updateSecuritySystems ( dtVo ) ;	
		}
		else { //save
			proId = sss.saveSecuritySystems ( dtVo ) ; 
		}
		
		//upload

		return proId ; 
	}		
	
	public  ActionForward removeSecuritySystem( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//PARAMS
		SecuritySystemForm securitySystemForm = ( SecuritySystemForm ) form ; 
		Byte actStatus = securitySystemForm.getProActStatus ( ) ;
		Date offTime = securitySystemForm.getOffTime ( ) ; 
		//BIS
		SecuritySystemServiceImpl sss = ( SecuritySystemServiceImpl )getBean ( "securitySystemServiceImpl" ) ; 
		
		Long[] deleteIds = securitySystemForm.getDeleteId ( ) ;
		if ( deleteIds != null ) {
			for ( Long deleteId : deleteIds ) {
				SecurityDoor secrityDoor = sss.getSecuritySystem ( deleteId ) ;
				Byte bos = secrityDoor.getProOnOffStatus ( ) ; 
				if ( Const.PRO_ACTION_PUB == actStatus ) {
					if ( ! NumberUtils.isEmptyOrZero ( bos )
							&& Const.PRO_PUB_STATUS_PUBED != bos.byteValue ( ) ) {
						if ( offTime == null
								|| ( offTime != null && ( new Date ( )
										.getTime ( )- offTime.getTime ( ) < 0 ) ) ) {						
							secrityDoor.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUBED ) ; 
							secrityDoor.setOffTime ( offTime ) ;
							sss.updateSecuritySystems ( secrityDoor ) ; 
						}
					}
				}
				else if ( Const.PRO_ACTION_OUT == actStatus ) { 
					if ( ! NumberUtils.isEmptyOrZero ( bos )
							&& Const.PRO_PUB_STATUS_PUBED == bos.byteValue ( ) ) {					
						secrityDoor.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ; 
						sss.updateSecuritySystems ( secrityDoor ) ; 
					}
				}
				else { //delete
					/*
					secrityDoor.setProOnOffStatus ( Const.PRO_PUB_STATUS_PUB_OUT ) ; 
					secrityDoor.setRemoveMark ( String.valueOf ( Const.COMMON_REMOVE_MARK_Y ) ) ; 
					sss.updateSecuritySystems ( secrityDoor ) ; */
					sss.removeSecuritySystem ( deleteId ) ; 
				}
			}
		}
		//Context.indexReload ( ) ; 
		return new ActionForward( "securitySystem.go?method=getSecuritySystem" , true ); 
	}
	
	
	//////////////////////////
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getSecuritySystems4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_SS_Type_KEY ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , getSessionLanguage( request ) ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;

		//BIS
		SecuritySystemServiceImpl sss = ( SecuritySystemServiceImpl )getBean ( "securitySystemServiceImpl" ) ;
		
		SecuritySystemForm securitySystemForm = ( SecuritySystemForm ) form ; 
		SecurityDoor secrityDoor = new SecurityDoor( ) ; 
		PropertyUtils.copyProperties ( secrityDoor , securitySystemForm ) ; 
		DataList dl = sss.getSecuritySystems4View ( secrityDoor ) ;
		List< SecurityDoor  > sds = dl.getNowlist ( ) ; 
		SecuritySystemType ssType = null ; 
		for ( SecurityDoor sd : sds ) {
			sd.setSsType ( null  ) ; 
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						sd.getSdtypeId ( ) ) ) {
					ssType = new SecuritySystemType( ) ; 
					ssType.setSdTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					ssType.setSdTypeName ( bioCt.getOptionName ( ) ) ;
					sd.setSsType ( ssType ) ;
				}
			}				
		}
		
		request.setAttribute ( "dl" , dl ) ;  
		return mapping.findForward ( "sDoors4View" ) ; 
	}
	
	
	@ SuppressWarnings ( "unchecked" )
	public  ActionForward getSecuritySystem4View( ActionMapping mapping , ActionForm form ,
			HttpServletRequest request , HttpServletResponse response ) throws Exception {
		response.setContentType ( "text/html;charset="
				+ AppContext.getFrameWorkPropertiesBean ( ).getCharSet ( ) );
		
		//DIC 
		List< BizOption >  bssOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_SS_Type_KEY ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bssOps" , bssOps ) ;
		
		List< BizOption >  bproOps = DictionaryCache.getInstance ( ).listBizOptions( Const.DIC_B_PRO_PUB_STATUS_KEY ,  false  , getSessionLanguage( request )  ) ;
		request.setAttribute ( "bproOps" , bproOps ) ;

		
		//BIS
		SecuritySystemServiceImpl sss = ( SecuritySystemServiceImpl )getBean ( "securitySystemServiceImpl" ) ;
		ProContentServiceImpl proContentServiceImpl = ( ProContentServiceImpl )getBean( "proContentServiceImpl" ) ; 		
		
		
		SecuritySystemForm securitySystemForm = ( SecuritySystemForm ) form ; 
		securitySystemForm.setPageSize (  Const.PAGE_SIZE_Q  ) ;
		SecurityDoor secrityDoor = new SecurityDoor( ) ; 
		PropertyUtils.copyProperties ( secrityDoor , securitySystemForm ) ; 
		Long proId = secrityDoor.getProId ( ) ; 
		String flg4boc = securitySystemForm.getFlg4boc ( ) ; 
		
		boolean showModal = false ;
		if ( secrityDoor.getShowModal ( ) != null
						&& secrityDoor.getShowModal ( ) )
			showModal = true ; 
		
		
		if ( Const.FLG_FOR_GET_CONTENTINFO.equals ( flg4boc ) ) {  
			ProContent pcontent = proContentServiceImpl.getProContentCacheModel (  
					new ProContent( Byte.valueOf ( Const.PRO_TYPE_SS ) ,  proId  )  ) ;
			if ( pcontent != null ) 
				return PorJsonObj.response ( response , PorJsonObj
						.getProContentJSONObject ( pcontent.getContent ( ) ).toString ( ) );
			else 
				return PorJsonObj.response ( response , PorJsonObj
						.getProContentJSONObject ( "" ).toString ( ) );		
		 
		}
		else if ( Const.FLG_FOR_GET_BASEINFO.equals ( flg4boc ) ) {  
			if ( NumberUtils.isEmptyOrZero(  proId) ) 
				return mapping.findForward ( "proout" );	
			if ( showModal )
				secrityDoor = sss.getSecuritySystem ( proId ) ;
			else 
				secrityDoor = sss.getBaseSecuritySystemById4View ( proId ) ;
			
			if ( secrityDoor == null ) 
				return mapping.findForward ( "proout" );	
			
			for ( BizOption bioCt : bssOps ) {
				if ( Byte.valueOf ( bioCt.getOptionValue ( ) ) .equals ( 
						secrityDoor.getSdtypeId ( ) ) ) {
					SecuritySystemType ssType = new SecuritySystemType( ) ; 
					ssType.setSdTypeId ( Byte.valueOf ( bioCt.getOptionValue ( ) ) ) ; 
					ssType.setSdTypeName ( bioCt.getOptionName ( ) ) ;
					secrityDoor.setSsType ( ssType ) ;
					break ; 
				}
			}		
			request.setAttribute ( "vo" , secrityDoor ) ;
			return mapping.findForward ( "sDoor4View" ) ;
			
		}else 
			return PorJsonObj.response ( response , PorJsonObj.error (
					CONST.COUNTRY_ZH , response  , Const.AJAX_ERROR_LOAD_PRO_NOTFOUND ).toString ( ) );						
	}
	
	
}
